$(document).ready(function () {
    //获取url
    var loc = location.href;
    var n1 = loc.length;//地址的总长度
    var n2 = loc.indexOf("=");//取得=号的位置
    var id = decodeURI(loc.substr(n2 + 1, n1 - n2));//从=号后面的内容


    var orderNo;
    var pay;
    $.ajax({
        /*url: "./json/order/create.json!shippingId=7.json",*/
        url: "http://" + urla + "/GetOrderByIdServlet",
        type: "GET",
        data:{
            "order_id":id,
        },
        dataType: 'JSON',
        success: function (result) {
            var payPrice = 0;
            console.log(result);
            orderNo = result.order_id;
                $.each(result.good, function (x, obj) {
                    console.log(obj)

                    var $i1 = $("<div class='i1'><div class='ck'><div class='w-chkbox'></div></div></div>");
                    var $i2 = $("<div class='i2'></div>");
                    var mainUrl = obj.good_url.split(",")[0]
                    var $pic = $("<div class='pic'><img src='"+mainUrl+"'></div>");
                    var $nameCon = $("<div class='nameCon'><a href='item.html?id="+obj.good_id+"'>" + obj.good_name + "</a></div>");
                    var $i3 = $("<div class='i3'><span>¥</span><span>" + obj.good_price + "</span></div>");
                    var $i4 = $("<div class='i4'><span>" + result.good_amount[obj.good_id] + "件</span></div>");
                    var $i5 = $("<div class='i5'><p class='c-price'><span>¥</span><span>" +result.good_amount[obj.good_id]*obj.good_price  + "</span></p></div>");
                    payPrice = payPrice+result.good_amount[obj.good_id]*obj.good_price;
                    var $cartitem = $("<div class='cart-item'></div>");
                    $i2.append($pic, $nameCon);
                    $cartitem.append($i1, $i2, $i3, $i4, $i5);
                    $(".cart-group").append($cartitem);
                });
            if (result.status==true){
                $("#info-bottom").remove();
            }
            $(".orderNo").text("订单编号: " + result.order_id);
            $(".orderStatus").text("订单状态: " + result.status);
            $(".address-info span:nth-child(1)").text("收货人：" + result.receiver_address.receiver);
            $(".address-info span:nth-child(2)").text(result.receiver_address.receiver_address);
            $(".address").text("收货地址： " + result.receiver_address.province + " "
                + result.receiver_address.city + " " + result.receiver_address.full_address);
            if (result.pay){
                $(".orderStatus").text("已支付");
            }else {
                $(".orderStatus").text("未支付");
            }

            console.log(payPrice);
            $("#priceNum").text(payPrice);
            pay = payPrice;

            if (result.pay){
                $("#payed").text("已支付：￥");
            }

        }
    });

    var successModal=$('#successModal');
    var unknownModal=$('#unknownModal');

    //ajax轮询
    function request() {
        $.ajax({
            type: "GET",
            url: "http://" + urla + "/OrderPayStatusServlet",
            data:{
                "order_id":orderNo,
            },
            dataType: 'JSON',
            success: function (data) {
                console.log("success");
                console.log(data.data);
                if (data.data === true) {
                    /*alert("付款成功，跳转至订单页！");*/
                    $("#myModal").modal('hide');
                    successModal.modal('show');
                    successModal.css("padding-right","0");
                    successModal.on('hide.bs.modal', function () {
                        window.location.replace("user_order.html");
                    })
                } else {
                    console.log(data);
                }
            },
            error: function (result) {
                console.log("error");
                console.log(result);
            }
        });
    }

    $("#paySub").click(function () {
        window.location.href="http://" + urla + "/AliPayServlet?order_id="+orderNo+"&totalPrice="+pay;
        // $.ajax({
        //     type: "GET",
        //     url: "http://" + urla + "/AliPayServlet",
        //     data: {
        //         "order_id":orderNo,
        //     },
        //     // dataType: 'JSON',
        //     // success: function (data) {
        //     //     console.log("success");
        //     //     console.log(data);
        //     //     $("#payCanvas").attr("src", data.data.qrURL);
        //     //     window.setInterval(request, 2000);
        //     // },
        //     // error: function (result) {
        //     //     console.log("error");
        //     //     console.log(result);
        //     // }
        // });
    });



});